package com.tcgroup.phone.base_core.store;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tcgroup.phone.base_core.utils.gson.ParameterizedTypeImpl;
import com.tencent.mmkv.MMKV;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/19.
 * 包名: com.tcgroup.phone.base_core.store
 * 类名: OSAndroidMvvm
 */
public class MmkvStore {

    private static MMKV mmkv;

    private MmkvStore() {
        mmkv = MMKV.defaultMMKV();
    }

    private static class MMKVHolder{
        private static MmkvStore mmkv = new MmkvStore();
    }

    public static MmkvStore getInstance(){
        return MMKVHolder.mmkv;
    }

    public MMKV getMmkv() {
        return mmkv;
    }

    /**
     * 存储 map
     * @param key 标识
     * @param map 数据集合
     */
    public void saveMapInfo(String key, Map<String, Object> map){
        Gson gson = new Gson();
        JSONArray mJsonArray = new JSONArray();
        JSONObject object = null;
        try {
            object = new JSONObject(gson.toJson(map));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mJsonArray.put(object);
        mmkv.encode(key,mJsonArray.toString());
    }

    public Map<String,String> getMapInfo(String key){
        String stored = mmkv.getString(key,"");
        Map<String, String> itemMap = new HashMap<>();
        try {
            JSONArray jsonArray = new JSONArray(stored);
            for (int i=0;i<jsonArray.length();i++){
                JSONObject itemObject = jsonArray.getJSONObject(i);
                JSONArray names = itemObject.names();
                if (names != null) {
                    for (int j = 0; j < names.length(); j++)
                    {
                        String name = names.getString(j);
                        String value = itemObject.getString(name);
                        itemMap.put(name, value);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return itemMap;
    }

    public <T> T getClassInfo(String key,Class<T> t){
        String str = mmkv.decodeString(key, null);
        if (!TextUtils.isEmpty(str)) {
            return new GsonBuilder().create().fromJson(str, t);
        } else {
            return null;
        }
    }

    public void saveClassInfo(String key, Object object) {
        String jsonString = new GsonBuilder().create().toJson(object);
        mmkv.encode(key, jsonString);
    }

    public <T> void saveListInfo(String key, ArrayList<T> list){
        String json = new Gson().toJson(list);
        mmkv.encode(key,json);
    }

    public <T> ArrayList<T> getListInfo(String key,Class<T> tClass){
        String str = mmkv.decodeString(key, null);
        if (!TextUtils.isEmpty(str)) {
            Type listType = new ParameterizedTypeImpl(ArrayList.class, new Class[]{tClass});
            return new Gson().fromJson(str, listType);
        } else {
            return new ArrayList<>();
        }

    }

    public void removeValueForKey(String key){
        mmkv.removeValueForKey(key);
    }

}
